package itbour.onetouchshow.activity.challengezone.customvideopreview;

import android.os.Environment;

import com.google.gson.Gson;
import com.lsjr.callback.DownloadSubscriber;
import com.lsjr.callback.EncryBeanCallBack;
import com.lsjr.utils.HttpUtils;

import java.io.File;
import java.util.HashMap;

import itbour.onetouchshow.App;
import itbour.onetouchshow.AppCache;
import itbour.onetouchshow.base.mvp.BasePresenterImpl;
import itbour.onetouchshow.bean.DownloadVideoBean;
import itbour.onetouchshow.constants.AppConfig;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.AlbumNotifyHelper;
import itbour.onetouchshow.utils.DateUtils;
import itbour.onetouchshow.utils.DeviceUtil;
import itbour.onetouchshow.utils.FileUtils;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.T_;
import itbour.onetouchshow.utils.UIUtils;


public class CustomVideoPreviewPresenter extends BasePresenterImpl<CustomVideoPreviewContract.View> implements CustomVideoPreviewContract.Presenter {

    /**
     * 作品预览  new
     */
    public void newLoadDocPreview(String docId) {
        L_.e("作品预览" + docId);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("docId", docId);
        HttpUtils.getInstance().executePost(AppConfig.PREVIEWCUSTOMVIDEODOC, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                T_.showCustomToast(exception);
                L_.e("newLoadDocPreview" + exception);
                if (mvpView != null) {
                    mvpView.loadFaild(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                if (mvpView != null) {
                    L_.e("newLoadDocPreview" + response);
                    mvpView.loadSucceed(response);
                }
            }
        });
    }


    public void getDownVideoUrl(String docId) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", 610);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("docId", docId);
        HttpUtils.getInstance().executeGet(AppConfig.GETDOCDOWNLOADURL_V1_0, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                T_.showToastReal(exception);
                if (mvpView != null) {
                    mvpView.loadFaild(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                L_.e("response" + response);

                if (mvpView != null) {
                    DownloadVideoBean downloadVideoBean = new Gson().fromJson(response, DownloadVideoBean.class);
                    // doDonwload(downloadVideoBean.getDownloadUrls().get(0));
                    mvpView.getDownVideoUrlresult(downloadVideoBean.getDownloadUrls().get(0));
                }
            }
        });
    }


    public void doDonwload(String url) {
        String name = DateUtils.getYMD() + FileUtils.getFileName(url);
        //先查找本地   必须保证在我们自己的目录下打开才能找到  如果自己发出相当于重新下载一次
        boolean downLoad = true;
        String path = Environment.getExternalStorageDirectory() + "/OneTouchShow";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        if (AppCache.getInstance().fileExists(path, name)) {
            downLoad = false;
        }
        if (downLoad) {
            L_.e("下载文件" + name);
            HttpUtils.getInstance().downloadFile(url, new DownloadSubscriber(UIUtils.getContext(),
                    null, name) {
                @Override
                public void onComplete(String path) {
                    L_.e("文件保存路径：" + path);
                    T_.showCustomToast("文件保存路径：" + path);
                    AlbumNotifyHelper.insertVideoToMediaStore(UIUtils.getContext(), String.valueOf(System.currentTimeMillis()), 0, 0);
                    if (mvpView != null) {
                        mvpView.downLoadVideoSuccess(path);
                    }
                }

                @Override
                public void update(long bytesRead, long contentLength, boolean done) {
                    int progress = (int) (bytesRead * 100 / contentLength);
                    L_.e(progress + "% ");
                }

                @Override
                protected void onXError(String exception) {
                    L_.e(exception + "% ");
                    T_.showToastReal(exception);
                    if (mvpView != null) {
                        mvpView.loadFaild(exception);
                    }
                }
            });
        } else {
            path = path + File.separator + name;
            path = path.replaceAll("//", "/");
            if (mvpView != null) {
                mvpView.downLoadVideoSuccess(path);
            }
        }

    }


    public void doReNameWork(String docId, String name) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("docId", docId);
        stringObjectHashMap.put("name", name);
        HttpUtils.getInstance().executeGet(AppConfig.RENAMEUSERDOC_V1_0, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.e(exception);
                T_.showCustomToast(exception);
            }

            @Override
            protected void onSuccess(String response) {
                L_.e(response);
            }
        });
    }


    /**
     * 获取自由创建配置项
     */

    public void getCustomVideoConfigure(String docId) {
        HashMap<String, Object> requestHashMap = new HashMap<>();
        requestHashMap.put("device", "620");
        requestHashMap.put("userId", "65");
        String appVersionName = DeviceUtil.getAppVersionName(UIUtils.getContext());
        requestHashMap.put("appVer", appVersionName);
        HttpUtils.getInstance().executeGet(AppConfig.GET_CUSTOM_VIDEO_CONFIGURE, requestHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.i("response  CreatConfigInfo===" + exception);
                if (mvpView != null) {
                    mvpView.loadFaild(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                L_.i("response  CreatConfigInfo===" + response);
                if (mvpView != null) {
                    mvpView.getCustomVideoConfigureResutl(response, docId);
                }
            }
        });

    }


    /**
     * 获取自由创建数据
     */
    public void getCustomVideodoccontent(String docId) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("docId", docId);
        HttpUtils.getInstance().executeGet(AppConfig.CUSTOMVIDEODOCCONTENT, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                //T_.showToastReal(exception);
                if (mvpView != null) {
                    mvpView.loadFaild(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                //T_.showToastReal(response);
                if (mvpView != null) {
                    mvpView.getCustomVideodoccontentResult(response);
                }

                L_.e(response);
            }
        });

    }


}
